package cn.labbook.bluetooth.controller;


import cn.labbook.bluetooth.common.ResultDto;
import cn.labbook.bluetooth.common.TokenManager;
import cn.labbook.bluetooth.entity.AmmeterReading;
import cn.labbook.bluetooth.entity.AmmeterReadingAmmeterView;
import cn.labbook.bluetooth.entity.TenantAmmeterPayView;
import cn.labbook.bluetooth.entity.excel.AmmeterReadingAmmeterViewTemplate;
import cn.labbook.bluetooth.entity.excel.TenantPayTemplate;
import cn.labbook.bluetooth.service.IAmmeterReadingAmmeterViewService;
import cn.labbook.bluetooth.service.IAmmeterReadingService;
import cn.labbook.bluetooth.util.DownloadUtils;
import cn.labbook.bluetooth.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lonzy
 * @since 2019-11-06
 */
@Controller
@RequestMapping("//ammeterReading")
public class AmmeterReadingController extends AbstractController{
    @Autowired
    IAmmeterReadingAmmeterViewService ammeterReadingAmmeterViewService;

    @RequestMapping("page")
    @ResponseBody
    public ResultDto page(Page<AmmeterReadingAmmeterView> page,String ammeterId,String timeBegin,String timeEnd){
        LambdaQueryWrapper<AmmeterReadingAmmeterView> lambda = new QueryWrapper<AmmeterReadingAmmeterView>().lambda();
        lambda.eq(AmmeterReadingAmmeterView::getManagerId, TokenManager.getToken().getAdminId());
        lambda.eq(StringUtils.isNotBlank(ammeterId),AmmeterReadingAmmeterView::getAmmeterId,ammeterId);
        lambda.between(StringUtils.isNotBlank(timeBegin)&&StringUtils.isNotBlank(timeEnd),AmmeterReadingAmmeterView::getReadingTime,timeBegin,timeEnd);
        ammeterReadingAmmeterViewService.page(page,lambda);
        return ammeterReadingAmmeterViewService.page(page,lambda).getRecords()!=null?success(page):fail();
    }
    //记录导出
    @RequestMapping("guestRecharge")
    public ResponseEntity<byte[]> guestRecharge(String ammeterId, String timeBegin, String timeEnd) throws Exception {
        LambdaQueryWrapper<AmmeterReadingAmmeterView> lambda = new QueryWrapper<AmmeterReadingAmmeterView>().lambda();
        lambda.eq(AmmeterReadingAmmeterView::getManagerId, TokenManager.getToken().getAdminId());
        lambda.eq(StringUtils.isNotBlank(ammeterId),AmmeterReadingAmmeterView::getAmmeterId,ammeterId);
        lambda.between(StringUtils.isNotBlank(timeBegin)&&StringUtils.isNotBlank(timeEnd),AmmeterReadingAmmeterView::getReadingTime,timeBegin,timeEnd);
        List<AmmeterReadingAmmeterView> list = ammeterReadingAmmeterViewService.list(lambda);

        List<AmmeterReadingAmmeterViewTemplate> collect = list.stream().map(l -> AmmeterReadingAmmeterViewTemplate.build(l)).collect(Collectors.toList());
        Map<String,String> map = new HashMap();
        map.put("ammeterId","电表号");
        map.put("location","地址");
        map.put("readingTime","抄表时间");
        map.put("electricQuantity","抄表示数");
        map.put("useQuantity","用电量");
        return DownloadUtils.downloadExcel(AmmeterReadingAmmeterViewTemplate.class, collect, map, "用电记录");
    }
}
